package com.qf.service.impl;

import com.qf.dao.TbFirstPictureRepository;
import com.qf.pojo.entity.TbFirstPicture;
import com.qf.pojo.request.TbFirstPictureReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbFirstPictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbFirstPictureServiceImpl implements TbFirstPictureService {
    @Autowired
    TbFirstPictureRepository tbFirstPictureRepository;
    /**
     * 查询所有首页图片信息
     *
     * @param page 当前页
     * @param size 当前页显示行数
     * @return 响应对象
     */
    public BaseResponse findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<TbFirstPicture> all = tbFirstPictureRepository.findAll(of);
        List<TbFirstPicture> firstPictures = all.getContent();
        long total = all.getTotalElements();
        return new BaseResponse(1,"success",firstPictures,total);
    }

    /**
     * 删除某个图片信息
     *
     * @param pid 图片id
     * @return 响应对象
     */
    public BaseResponse deleteById(Integer pid) {
        tbFirstPictureRepository.deleteById(pid);
        return new BaseResponse().SUCCESS();
    }

    /**
     * 修改或者新增图片
     *
     * @param tbFirstPictureReq 图片请求对象
     * @return 响应对象
     */
    public BaseResponse updateOrSave(TbFirstPictureReq tbFirstPictureReq) {
        TbFirstPicture tbFirstPicture = new TbFirstPicture();
        BeanUtils.copyProperties(tbFirstPictureReq,tbFirstPicture);
        tbFirstPictureRepository.saveAndFlush(tbFirstPicture);
        return new BaseResponse().SUCCESS();
    }
}
